package com.blockmeta.bbs.businesslibrary.net.okhttp;

import android.content.Context;

import com.blockmeta.bbs.baselibrary.net.callback.Callback;
import com.blockmeta.bbs.baselibrary.utils.SharedPreferenceUtil;
import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.baselibrary.utils.gson.GsonUtil;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.net.callback.ApiStringCallBack;
import com.blockmeta.bbs.businesslibrary.net.pojo.ErrorPOJO;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * Created by author on 2018/1/25.
 */

public class OkHttpPostString {
    public static void postString(final Context context, String url, HashMap<String, String> map, final Callback callback) {
        OkHttpUtils.postString()
                .addHeader("from", "android")
                .addHeader("token", SharedPreferenceUtil.getString(context, Constant.TOKEN, ""))
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(new Gson().toJson(map))
                .url(url)
                .build()
                .execute(new ApiStringCallBack(context) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        if (code == 200) {
                            callback.onResponse(response, id);
                        } else if (code == 300) {
                            ErrorPOJO errorPOJO = GsonUtil.buildGson().fromJson(response, ErrorPOJO.class);
                            ToastUtil.show(errorPOJO.getMessage());
                        } else {
                            ToastUtil.show("联网失败，请检查网络");
                        }
                    }
                });
    }

    public static void postString(final Context context, String url, HashMap<String, String> map, String key, String value, final Callback callback) {
        OkHttpUtils.postString()
                .addHeader("from", "android")
                .addHeader("token", SharedPreferenceUtil.getString(context, Constant.TOKEN, ""))
                .addHeader("uid", value)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(new Gson().toJson(map))
                .url(url)
                .build()
                .execute(new ApiStringCallBack(context) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        if (code == 200) {
                            callback.onResponse(response, id);
                        } else if (code == 300) {
                            ErrorPOJO errorPOJO = GsonUtil.buildGson().fromJson(response, ErrorPOJO.class);
                            ToastUtil.show(errorPOJO.getMessage());
                        } else {
                            ToastUtil.show("联网失败，请检查网络");
                        }
                    }
                });
    }
}
